package com.atguigu.statisticsservice.service.impl;

import com.atguigu.statisticsservice.client.MemberClient;
import com.atguigu.statisticsservice.entity.StatisticsDaily;
import com.atguigu.statisticsservice.mapper.StatisticsDailyMapper;
import com.atguigu.statisticsservice.service.StatisticsDailyService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.swagger.models.auth.In;
import org.apache.commons.lang3.RandomUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 网站统计日数据 服务实现类
 * </p>
 *
 * @author kaixuan
 * @since 2020-06-15
 */
@Service
public class StatisticsDailyServiceImpl extends ServiceImpl<StatisticsDailyMapper, StatisticsDaily> implements StatisticsDailyService {

    @Autowired
    private MemberClient memberClient;

    /**
     * 创建统计数据
     * @param day
     */
    public void createStatistics(String day){
        //删除已存在的统计数据
        QueryWrapper<StatisticsDaily> query = new QueryWrapper<>();
        query.eq("date_calculated",day);
        baseMapper.delete(query);

        //获取统计数据
        Integer registerNum = (Integer) memberClient.registerCount(day).getData().get("row");//每日注册人数
        Integer loginNum = RandomUtils.nextInt(100,200);//每日登陆人数 TODO
        Integer videoViewNum  = RandomUtils.nextInt(100,200);//每日播放数数 TODO
        Integer courseNum  = RandomUtils.nextInt(100,200);//每日新增课程数数 TODO

        StatisticsDaily daily = new StatisticsDaily();
        daily.setRegisterNum(registerNum);
        daily.setLoginNum(loginNum);
        daily.setVideoViewNum(videoViewNum);
        daily.setCourseNum(courseNum);
        daily.setDateCalculated(day);
        baseMapper.insert(daily);
    }

    @Override
    public Map<String, Object> showChart(String begin, String end, String type) {
        QueryWrapper<StatisticsDaily> query = new QueryWrapper<>();
        query.select(type,"date_calculated");
        query.between("date_calculated",begin,end);
        List<StatisticsDaily> dayList = baseMapper.selectList(query);

        Map<String, Object> map = new HashMap<>();
        List<Integer> dataList = new ArrayList<Integer>();
        List<String> dateList = new ArrayList<String>();
        map.put("dataList", dataList);
        map.put("dateList", dateList);
        for (StatisticsDaily daily : dayList) {
            dateList.add(daily.getDateCalculated());
            switch (type) {
                case "register_num":
                    dataList.add(daily.getRegisterNum());
                    break;
                case "login_num":
                    dataList.add(daily.getLoginNum());
                    break;
                case "video_view_num":
                    dataList.add(daily.getVideoViewNum());
                    break;
                case "course_num":
                    dataList.add(daily.getCourseNum());
                    break;
                default:
                    break;
            }
        }
        return map;
    }
}
